#ifndef OPTIMIZER_H
#define OPTIMIZER_H

#include <map>
#include <string>
#include <vector>
#include "ASTNode.h"

// 常量折叠优化器
class ConstantFoldingOptimizer {
private:
    bool isConstant(ASTNode* node);
    int evaluateConstant(ASTNode* node);
    
public:
    void optimize(ASTNode* root);
    void optimizeNode(ASTNode* node);
};

// 公共子表达式消除优化器
class CommonSubexpressionOptimizer {
private:
    std::map<std::string, ASTNode*> expressionMap;
    std::string getExpressionKey(ASTNode* node);
    
public:
    void optimize(ASTNode* root);
    void optimizeNode(ASTNode* node);
};

// 死代码消除优化器
class DeadCodeEliminator {
private:
    bool isDeadCode(ASTNode* node);
    bool hasSideEffects(ASTNode* node);
    
public:
    void optimize(ASTNode* root);
    void optimizeNode(ASTNode* node);
};

// 主优化器
class Optimizer {
private:
    ConstantFoldingOptimizer constantFolder;
    CommonSubexpressionOptimizer cseOptimizer;
    DeadCodeEliminator deadCodeEliminator;
    
public:
    void optimize(ASTNode* root);
    void optimizeConstantFolding(ASTNode* root);
    void optimizeCommonSubexpressions(ASTNode* root);
    void optimizeDeadCodeElimination(ASTNode* root);
};

#endif // OPTIMIZER_H 